package com.example.artwindow.controller;

import com.example.artwindow.domain.po.MyUserDetails;
import com.example.artwindow.domain.po.User;
import com.example.artwindow.domain.dto.UserLoginDTO;
import com.example.artwindow.domain.dto.UserUpdateDTO;
import com.example.artwindow.domain.vo.UserInfoVO;
import com.example.artwindow.domain.vo.UserLoginVO;
import com.example.artwindow.domain.dto.UserRegisterDTO;
import com.example.artwindow.result.Result;
import com.example.artwindow.service.UserService;
import com.example.artwindow.tool.QiNiuOSTool;
import com.example.artwindow.tool.TheadLocalTool;
import io.jsonwebtoken.lang.Objects;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/user")
@Tag(name="用户相关接口")
@Slf4j
@RequiredArgsConstructor
public class UserController {

	private final UserService userService;
	@Resource
	private PasswordEncoder passwordEncoder;
	@Resource
	private AuthenticationManager authenticationManager;

	@Operation(summary = "用户注册接口")
	@PostMapping(value = "/register")
	private Result<?> userRegister(@RequestBody UserRegisterDTO userRegisterDTO){
		if (userRegisterDTO.getPassword().length()<6){
			return Result.error("密码长度过低",403);
		}
		//加密密码
		userRegisterDTO.setPassword(passwordEncoder.encode(userRegisterDTO.getPassword()));
		return userService.userRegister(userRegisterDTO);
	};



	@Operation(summary = "用户登录接口")
	@PostMapping(value = "/login")
	private Result<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO){
		UsernamePasswordAuthenticationToken authenticationToken=new UsernamePasswordAuthenticationToken(userLoginDTO.getUsername(),userLoginDTO.getPassword());
		Authentication authenticate = authenticationManager.authenticate(authenticationToken);
		SecurityContextHolder.getContext().setAuthentication(authenticate);
		MyUserDetails myUserDetails = (MyUserDetails) authenticate.getPrincipal();
		return userService.userLogin(myUserDetails.getUser());
	};

	@Operation(summary = "查询个人信息")
	@GetMapping(value = "/{id}")
	private Result<UserInfoVO> userSearch(@PathVariable Long id){
		return userService.userSearch(id);
	}

	@Operation(summary = "修改个人信息")
	@PutMapping(value = "/user",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	private Result<Null> userUpdate(UserUpdateDTO userUpdateDTO,
	                                @RequestPart(value = "profilePhoto",required = false)MultipartFile profilePhoto){
		if(!Objects.isEmpty(userUpdateDTO.getPassword())){
			userUpdateDTO.setPassword(passwordEncoder.encode(userUpdateDTO.getPassword()));
		}
		return userService.userUpdate(userUpdateDTO,profilePhoto);
	}

	@Operation(summary = "删除账户")
	@DeleteMapping("/user")
	private Result<Null> userDelete(){
		return userService.userDelete();
	}

	@Operation(summary = "上传图片")
	@PostMapping(value = "/bgi",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	private Result<String> bgi(@RequestPart(value = "bgi",required = true)MultipartFile bgi){
		String os = QiNiuOSTool.Os(String.valueOf((TheadLocalTool.getCurrentId() + System.currentTimeMillis())), bgi);
		return Result.success(os);
	}
}
